-- trans_to_skeleton
-- create by xinj
-- 变骷髅

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 对BOSS无效
        if target:isBoss() then
            return {};
        end

        -- 免疫该状态，也不处理了
        if CombatStatusM.immunityStatus(target, "skeleton") then
            SkillM.getSequence(target):immunity(source, target, skillId);
            return {};
        end

        -- 禁锢抗性；skill里处理了部分效果，要先判断禁锢抗性
        local prop = PropM.combine(target, "durance_resist", 1);
        if PropM.apply(prop, 1) > 0 then
            if target.isAudience and target:isAudience() then
                -- 观众就不要提示了
            elseif not target:isDead() then
                if prop[4] == -1 then
                    doAlert(getLocStr("no_durance2"));
                else
                    doAlert(string.format(getLocStr("no_durance"), (prop[4] - round)));
                end
            end
            return {};
        end

        -- 无法被变形的
        if target.type == OBJECT_TYPE_MONSTER then
            local classId = target:getClassId();
            local dbase = MonsterM.query(classId, "dbase") or {};
            if 1 == dbase["cant_be_trans"] then
                return {};
            end
        end

        local effectRound = -1;
        extra = extra or {};
        local lastRound = extra.lastRound;

        if type(lastRound) == "number" then
            effectRound = round + lastRound + 1;
        elseif para[2] > 0 then
            local v = para[2];

            -- 不包括本回合
            effectRound = round + v + 1;
        end

        prop = PropM.combine(source, "magic_last", skillId);
        effectRound = PropM.apply(prop, effectRound);

        -- 附加骷髅状态
        local condition = {
            ["end_round"] = effectRound,
        };

        CombatStatusM.applyStatus(target, "skeleton", condition);

        -- 清除增益状态（只清除buff）
        CombatStatusM.clearStatusByType(target, COM_STATUS_BUFF);
        -- 清除所有增益属性(debuff不管)
        target.dbase:deleteTemp("prop", "buff");
        PropM.deleteProp(target, "skill", false);
        target.dbase:deleteTemp("prop", "equip");
        target.dbase:deleteTemp("prop", "special");

        -- 增加命中动作
        SkillM.getSequence(target):hit(source, target, skillId);

        -- 改变模型
        if target.type == OBJECT_TYPE_MONSTER and not target:isDead() then
            -- 怪物才变骷髅
            local dbase = MonsterM.query(target.classId, "dbase") or {};
            local changeClassId = 4;

            if target:isSlime() or dbase["slime_display"] == 1 then
                -- 冈布奥怪特殊处理
                changeClassId = 6;
            end

            if FormulaM.invoke("CAN_MONSTER_CHANGE_MODEL", target) then
                local pos = target.dbase:query("pos");
                if not pos then
                    pos = target:getOwner();
                end
                EventMgr.fire(event.GRID_CHANGE_MODEL,
                    {["pos"] = pos, ["changeClassId"] = changeClassId});
            end
        end

        EventMgr.fire(event.GRID_LOCK_STATE, {["pos"] = target:getPos(), });

        -- 作用目标
        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), tonumber(arr[2] or -1) };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        return desc;
    end,
};
